package com.maxd.modules.member.service;

import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.modules.member.entity.Member;
import com.maxd.model.UserInfo;
import com.maxd.modules.member.dao.MemberRepository;
import com.maxd.respository.userinfo.UserJpaRepository;
import com.maxd.respository.userinfo.UserRepository;
import com.maxd.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class MemberServiceImpl implements IMemberService {

    @Autowired
    private MemberRepository memberRepository;
    @Autowired
    private UserJpaRepository userJpaRepository;
    @Autowired
    private UserRepository userRepository;

    @Override
    public Result saveMember(Member member){
        member.setCreateTime(DateUtil.createTime());
        memberRepository.save(member);
        return ResultUtil.success();
    }

    @Override
    public Result deleteMember(Long memberId){
        memberRepository.deleteById(memberId);
        return ResultUtil.success();
    }

    @Override
    public Result selectMemberList(Integer page,Integer size){
        if(page==null || size==null){
            return ResultUtil.success(memberRepository.findAll());
        }
        Sort.Order order = new Sort.Order(Sort.Direction.ASC, "grade");
        Pageable pageable = PageRequest.of(page, size, Sort.by(order));
        return ResultUtil.success(memberRepository.findAll(pageable));
    }


    @Override
    public Result selectMemberListByUserId(Integer page,Integer size,Long userId){
        UserInfo userInfo = userJpaRepository.findById(userId).orElse(null);
        int myNum = userRepository.selectUserByZhiCount(userInfo.getRelationId());
        if(page==null || size==null){
            List<Member> all = memberRepository.findAll();
            for(Member member:all){
                int i = Integer.parseInt(userInfo.getGradeIndex());
                if(member.getGrade()>i){
                    member.setMinNum(member.getPeopleNum()-myNum);
                }else{
                    member.setMinNum(0);
                }
            }
            return ResultUtil.success(all);
        }
        Sort.Order order = new Sort.Order(Sort.Direction.ASC, "grade");
        Pageable pageable = PageRequest.of(page, size, Sort.by(order));
        Page<Member> all = memberRepository.findAll(pageable);
        List<Member> content = all.getContent();
        for(Member member:content){
            int i = Integer.parseInt(userInfo.getGradeIndex());
            if(member.getGrade()>i){
                member.setMinNum(member.getPeopleNum()-myNum);
            }else{
                member.setMinNum(0);
            }
        }
        return ResultUtil.success(all);
    }

    @Override
    public Result setUserMember(Long userId,Long memberId){
        UserInfo userInfo = userJpaRepository.findById(userId).orElse(null);
        Member member = memberRepository.findById(memberId).orElse(null);
        userInfo.setGrade(member.getRate());
        userInfo.setZhiRate(member.getZhiRate());
        userInfo.setFeiRate(member.getFeiRate());
        userInfo.setGradeIndex(String.valueOf(member.getGrade()));
        userInfo.setGradeTime(DateUtil.createTime());
        userJpaRepository.save(userInfo);
        return ResultUtil.success();
    }

    @Override
    public Member findMemberByPeopleNum(Integer peopleNum){
        return memberRepository.findMemberByPeopleNum(peopleNum);
    }


}
